<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="cart-reduce" v-show="food.count>0" @click.stop.prevent="reduceCount">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="cart-count" v-show="food.count>0">{{food.count}}</div>
    <div class="cart-add" @click.stop.prevent="addCount" >
      <span class="icon-add_circle"></span>
    </div>
  </div>

</template>

<script type="text/ecmascript-6">
  import Vue from 'vue';

  export default {
    name: "cartcontrol",
    props: {
      food: {
        type: Object
      }
    },
    mounted(){
      // console.log(this.food)
    },
    methods: {
      addCount() {
        if (!event._constructed) {
          return false;
        }
        if (!this.food.count) {
          Vue.set(this.food, 'count',0)
        }
        if(this.food.count >= 0){
          this.food.count = this.food.count + 1;
          // console.log(this.food.count)
        }
        //子组件向父组件传递方法
        this.$emit('cartAdd', event.target)//添加这句，提交'cart-add'事情给父组件goods，第二个是要传递的参数
      },
      reduceCount() {
        if (!event._constructed) {
          return false;
        }
        if(this.food.count>0){
          this.food.count = this.food.count - 1;
        }else{
          this.food.count  = 0
        }
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .cartcontrol
    display flex
    align-items center
    > div
      display inline-block
      padding 6px
      font-size 24px
      line-height 24px
    .cart-reduce
      opacity 1
      box-sizing border-box
      transform translateX(0)
      transform rotate(0)
      &.move-enter-active,
      &.move-leave-active
        transition all 0.4s linear
      .inner
        display inline-block
        color rgb(0, 160, 220)
      &.move-enter,
      &.move-leave-to
        opacity 0
        transform translateX(24px)
        transform rotate(180deg)
    .cart-count
      font-size 10px
      color: rgb(147, 153, 159)
      padding 0 6px
    .cart-add
      color rgb(0, 160, 220)


</style>
